# Guess, Andrew and Alexander Coppock. Does Counter-Attitudinal Information Cause Backlash? Results from Three Large Survey Experiments. British Journal of Political Science, forthcoming.

# Produces Table 1, Table 2, and Table 3 in the main text
# Produdes Tables 5 - 10 in the Appendix

# Study 1 (Gun Control)

rm(list = ls())
library(tidyverse)
library(reshape2)
library(estimatr)
library(stargazer)
library(xtable)

load("GC_study_1.rdata")
source("GC_helpers.R")

study_1 <- study_1 %>% filter(!is.na(Z_information))

# Table 1 -----------------------------------------------------------------

tab_1 <-
  study_1 %>%
  group_by(Z_information) %>%
  summarise(n = format_num(n(), 0))

tab_2 <-
  study_1 %>%
  group_by(Z_information) %>%
  summarize(
    W2_scale_mean = weighted.mean(W2_scale, weight2, na.rm = TRUE),
    W2_scale_sd = weighted_se(W2_scale, weight2, na.rm = TRUE),
    W2_Q1_mean = weighted.mean(W2_Q1, weight2, na.rm = TRUE),
    W2_Q1_sd = weighted_se(W2_Q1, weight2, na.rm = TRUE)
  ) %>%
  mutate_if(is_bare_numeric, format_num, digits = 2)

tab_3 <-
  map(1:1000, ~study_1 %>% sample_n(size = nrow(study_1), replace = TRUE)) %>%
  map(~ group_by(., Z_information) %>%
        summarize(
          W2_scale_mean = weighted.mean(W2_scale, weight2, na.rm = TRUE),
          W2_scale_sd = weighted_se(W2_scale, weight2, na.rm = TRUE),
          W2_Q1_mean = weighted.mean(W2_Q1, weight2, na.rm = TRUE),
          W2_Q1_sd = weighted_se(W2_Q1, weight2, na.rm = TRUE)
        )) %>%
  bind_rows(.id = "sim") %>%
  melt(id.vars = c("sim", "Z_information")) %>%
  dcast(Z_information ~ variable, fun.aggregate = sd) %>%
  mutate_if(is_bare_numeric, add_parens, digits = 2)

summary_df <-
  tab_1 %>%
  left_join(tab_2) %>%
  bind_rows(tab_3) %>%
  arrange(Z_information)

summary_df <-
  within(summary_df, {
    Z_information[is.na(n)] <- NA
  })


summary_df <-
  within(summary_df, {
    Z_information <- as.character(Z_information)
    Z_information[Z_information == "control"] <- "Control"
    Z_information[Z_information == "pro_information"] <- "Positive Information"
    Z_information[Z_information == "con_information"] <- "Negative Information"
  })


print.xtable(
  xtable(summary_df),
  hline.after = c(),
  include.rownames = FALSE,
  include.colnames = FALSE,
  only.contents = TRUE
)

# Table 2 -----------------------------------------------------------------

fit_1 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, pro == 0))
fit_2 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, pro == 0))
fit_3 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, pro == 1))
fit_4 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, pro == 1))

stargazer(
  fit_1, fit_2, fit_3, fit_4,
  se = starprep(fit_1, fit_2, fit_3, fit_4),
  p = fix_ps(fit_1, fit_2, fit_3, fit_4),
  style = "apsr", column.sep.width = "0pt", digits = 2,
  omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|XPARTY7|IDEO"),
  omit.labels = c("Covariates"),
  omit.stat = c("adj.rsq", "f", "ser"),
  column.labels = c("Among Opponents", "Among Proponents"),
  column.separate = c(2, 2),
  dep.var.labels = "Dependent Variable: Composite Scale",
  covariate.labels = c("Positive Information", "Negative Information", "Constant"),
  model.numbers = FALSE,
  notes = c(
    "Robust standard errors are in parentheses.",
    "Covariates include age, registation, education, Hispanic ethnicity,",
    "gender, income, marital status, employment status, party ID, and ideology."
  ),
  font.size = "small", float = FALSE
)


# Table 3 -----------------------------------------------------------------

fit_1 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, pro == 0))
fit_2 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, pro == 0))
fit_3 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, pro == 1))
fit_4 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7) + as.factor(IDEO), weights = weight2, data = filter(study_1, pro == 1))

stargazer(
  fit_1, fit_2, fit_3, fit_4,
  se = starprep(fit_1, fit_2, fit_3, fit_4),
  p = fix_ps(fit_1, fit_2, fit_3, fit_4),
  style = "apsr", column.sep.width = "0pt", digits = 2,
  omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|XPARTY7|IDEO"),
  omit.labels = c("Covariates"),
  omit.stat = c("adj.rsq", "f", "ser"),
  column.labels = c("Among Opponents", "Among Proponents"),
  column.separate = c(2, 2),
  dep.var.labels = "Dependent Variable: Support Gun Control",
  covariate.labels = c("Positive Information", "Negative Information", "Constant"),
  model.numbers = FALSE,
  notes = c(
    "Robust standard errors are in parentheses.",
    "Covariates include age, registation, education, Hispanic ethnicity,",
    "gender, income, marital status, employment status, party ID, and ideology."
  ),
  font.size = "small", float = FALSE
)

# Appendix Tables: Table 5 (Issue Importance) -----------------------------

fit_1 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, importance %in% c(1, 2)))
fit_2 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO + XPARTY7, weights = weight2, data = filter(study_1, importance %in% c(1, 2)))
fit_3 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, importance %in% c(3)))
fit_4 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working  + IDEO + XPARTY7, weights = weight2, data = filter(study_1, importance %in% c(3)))
fit_5 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, importance %in% c(4, 5)))
fit_6 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO + XPARTY7, weights = weight2, data = filter(study_1, importance %in% c(4, 5)))

stargazer(
  fit_1, fit_2, fit_3, fit_4, fit_5, fit_6,
  se = starprep(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
  p = fix_ps(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
  style = "apsr", column.sep.width = "0pt", digits = 2,
  omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|IDEO|PARTY"),
  omit.labels = c("Covariates"),
  omit.stat = c("adj.rsq", "f", "ser"),
  column.labels = c("Low Importance", "Medium Importance", "High Importance"),
  column.separate = c(2, 2, 2),
  dep.var.labels = "Dependent Variable: Support Gun Control",
  covariate.labels = c("Positive Information", "Negative Information", "Constant"),
  model.numbers = FALSE,
  notes = c(
    "Robust standard errors are in parentheses.",
    "Covariates include age, registation, education, hispanic ethnicity,",
    "gender, income, marital status, employment status, ideology and party ID."
  ),
  font.size = "small", float = FALSE
)

# Appendix Tables: Table 6 (Issue Importance) -----------------------------

fit_1 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, importance %in% c(1, 2)))
fit_2 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO + XPARTY7, weights = weight2, data = filter(study_1, importance %in% c(1, 2)))
fit_3 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, importance %in% c(3)))
fit_4 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working  + IDEO + XPARTY7, weights = weight2, data = filter(study_1, importance %in% c(3)))
fit_5 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, importance %in% c(4, 5)))
fit_6 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO + XPARTY7, weights = weight2, data = filter(study_1, importance %in% c(4, 5)))

stargazer(
    fit_1, fit_2, fit_3, fit_4, fit_5, fit_6,
    se = starprep(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
    p = fix_ps(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
    style = "apsr", column.sep.width = "0pt", digits = 2,
    omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|IDEO|PARTY"),
    omit.labels = c("Covariates"),
    omit.stat = c("adj.rsq", "f", "ser"),
  dep.var.labels = "Dependent Variable: Support Gun Control",
    column.labels = c("Low Importance", "Medium Importance", "High Importance"),
    column.separate = c(2, 2, 2),
    covariate.labels = c("Positive Information", "Negative Information", "Constant"),
    model.numbers = FALSE,
    notes = c(
      "Robust standard errors are in parentheses.",
      "Covariates include age, registation, education, hispanic ethnicity,",
      "gender, income, marital status, employment status, ideology and party ID."
    ),
    font.size = "small", float = FALSE
)

# Appendix Tables: Table 7 (Attitude Consistency) -------------------------

fit_1 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, consist == 0))
fit_2 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7), weights = weight2, data = filter(study_1, consist == 0))
fit_3 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, consist == 1))
fit_4 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7), weights = weight2, data = filter(study_1, consist == 1))

stargazer(
  fit_1, fit_2, fit_3, fit_4,
  se = starprep(fit_1, fit_2, fit_3, fit_4),
  p = fix_ps(fit_1, fit_2, fit_3, fit_4),
  style = "apsr", column.sep.width = "0pt", digits = 2,
  omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|XPARTY7|IDEO"),
  omit.labels = c("Covariates"),
  omit.stat = c("adj.rsq", "f", "ser"),
  column.labels = c("Not Consistent", "Consistent"),
  column.separate = c(2, 2),
  dep.var.labels = "Dependent Variable: Composite Scale",
  covariate.labels = c("Positive Information", "Negative Information", "Constant"),
  model.numbers = FALSE,
  notes = c(
    "Robust standard errors are in parentheses.",
    "Covariates include age, registation, education, Hispanic ethnicity,",
    "gender, income, marital status, employment status, and party ID."
  ),
  font.size = "small", float = FALSE
)



# Appendix Tables: Table 8 (Attitude Consistency) -------------------------

fit_1 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, consist == 0))
fit_2 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7), weights = weight2, data = filter(study_1, consist == 0))
fit_3 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, consist == 1))
fit_4 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + as.factor(XPARTY7), weights = weight2, data = filter(study_1, consist == 1))

sink("Written Drafts/BJPS_R&R/tables/gun_control_support_consistency.tex")
stargazer(
  fit_1, fit_2, fit_3, fit_4,
  se = starprep(fit_1, fit_2, fit_3, fit_4),
  p = fix_ps(fit_1, fit_2, fit_3, fit_4),
  style = "apsr", column.sep.width = "0pt", digits = 2,
  omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|XPARTY7|IDEO"),
  omit.labels = c("Covariates"),
  omit.stat = c("adj.rsq", "f", "ser"),
  column.labels = c("Not Consistent", "Consistent"),
  column.separate = c(2, 2),
  dep.var.labels = "Dependent Variable: Support Gun Control",
  covariate.labels = c("Positive Information", "Negative Information", "Constant"),
  model.numbers = FALSE,
  notes = c(
    "Robust standard errors are in parentheses.",
    "Covariates include age, registation, education, Hispanic ethnicity,",
    "gender, income, marital status, employment status, party ID, and ideology."
  ),
  font.size = "small", float = FALSE
)
sink()


# Appendix Tables: Table 9 (Party ID) -------------------------------------

fit_1 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, XPARTY7 %in% c(1, 2)))
fit_2 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO, weights = weight2, data = filter(study_1, XPARTY7 %in% c(1, 2)))
fit_3 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, XPARTY7 %in% c(3, 4, 5)))
fit_4 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO, weights = weight2, data = filter(study_1, XPARTY7 %in% c(3, 4, 5)))
fit_5 <- lm(W2_scale ~ Z_information, weights = weight2, data = filter(study_1, XPARTY7 %in% c(6, 7)))
fit_6 <- lm(W2_scale ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO, weights = weight2, data = filter(study_1, XPARTY7 %in% c(6, 7)))

stargazer(
  fit_1, fit_2, fit_3, fit_4, fit_5, fit_6,
  se = starprep(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
  p = fix_ps(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
  style = "apsr", column.sep.width = "0pt", digits = 2,
  omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|IDEO"),
  omit.labels = c("Covariates"),
  omit.stat = c("adj.rsq", "f", "ser"),
  column.labels = c("Among Democrats", "Among Independents", "Among Republicans"),
  column.separate = c(2, 2, 2),
  dep.var.labels = "Dependent Variable: Composite Scale",
  covariate.labels = c("Positive Information", "Negative Information", "Constant"),
  model.numbers = FALSE,
  notes = c(
    "Robust standard errors are in parentheses.",
    "Covariates include age, registation, education, hispanic ethnicity,",
    "gender, income, marital status, employment status, and ideology."
  ),
  font.size = "small", float = FALSE
)

# Appendix Tables: Table 10 (Party ID) ------------------------------------

fit_1 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, XPARTY7 %in% c(1, 2)))
fit_2 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO, weights = weight2, data = filter(study_1, XPARTY7 %in% c(1, 2)))
fit_3 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, XPARTY7 %in% c(3, 4, 5)))
fit_4 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working  + IDEO, weights = weight2, data = filter(study_1, XPARTY7 %in% c(3, 4, 5)))
fit_5 <- lm(W2_Q1 ~ Z_information, weights = weight2, data = filter(study_1, XPARTY7 %in% c(6, 7)))
fit_6 <- lm(W2_Q1 ~ Z_information + ppagecat + as.factor(PPREG4) + PPEDUCAT + hispanic + as.factor(PPGENDER) + PPINCIMP + as.factor(PPMARIT) + working + IDEO, weights = weight2, data = filter(study_1, XPARTY7 %in% c(6, 7)))

stargazer(
  fit_1, fit_2, fit_3, fit_4, fit_5, fit_6,
  se = starprep(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
  p = fix_ps(fit_1, fit_2, fit_3, fit_4, fit_5, fit_6),
  style = "apsr", column.sep.width = "0pt", digits = 2,
  omit = c("ppagecat|PPREG4|PPEDUCAT|hispanic|PPGENDER|PPINCIMP|PPMARIT|working|IDEO"),
  omit.labels = c("Covariates"),
  omit.stat = c("adj.rsq", "f", "ser"),
  column.labels = c("Among Democrats", "Among Independents", "Among Republicans"),
  column.separate = c(2, 2, 2),
  dep.var.labels = "Dependent Variable: Support Gun Control",
  covariate.labels = c("Positive Information", "Negative Information", "Constant"),
  model.numbers = FALSE,
  notes = c(
    "Robust standard errors are in parentheses.",
    "Covariates include age, registation, education, hispanic ethnicity,",
    "gender, income, marital status, employment status, and ideology."
  ),
  font.size = "small", float = FALSE
)

